﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using UTC_Project_Manager.Model.Infrastructure;


namespace UTC_Project_Manager.Controls
{
    /// <summary>
    /// Interaction logic for AppplicationSettingsControl.xaml
    /// </summary>
    public partial class AppplicationSettingsControl : UserControl
    {

        private Boolean isInit;
        public AppplicationSettingsControl()
        {
            InitializeComponent();
            isInit = true;
            disableButtons();
            populateFields();
            isInit = false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "XML files|*.xml";
            dialog.Title = "Choisissez le fichier à charger";
            dialog.ShowDialog();
            if (dialog.FileName != "")
            {
                App.MyConfig.LoadPath = dialog.FileName;
                this.pathLoadTextBox.Text = dialog.FileName;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Title = "Choisissez l'emplacement de sauvegarde";
            dialog.ShowDialog();
            if (dialog.FileName != "")
            {
                App.MyConfig.SavePath = dialog.FileName;
                this.pathSaveTextBox.Text = dialog.FileName;
            }
            
        }

        private void enableButtons()
        {
            this.saveServerSettingsButton.IsEnabled = true;
            
        }

        private void disableButtons()
        {
            this.saveServerSettingsButton.IsEnabled = false;
        }

        private void enableCredentials()
        {
            this.loginTextBox.IsEnabled = true;
            this.passwordTextBox.IsEnabled = true;

        }

        private void disableCredentials()
        {
            this.loginTextBox.IsEnabled = false;
            this.passwordTextBox.IsEnabled = false;

        }

        private void emailTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!isInit)
                enableButtons();
        }

        private void anonymeCheckBox_Click(object sender, RoutedEventArgs e)
        {
            enableButtons();
            if ((bool)this.anonymeCheckBox.IsChecked)
                disableCredentials();
            else
                enableCredentials();
        }

        private void saveServerSettingsButton_Click(object sender, RoutedEventArgs e)
        {
            disableButtons();
            Project project = App.MyConfig.OurProject;
            project.Email = this.emailTextBox.Text;
            try
            {
                MailSettings smtp = new MailSettings(this.smtpTextBox.Text, Convert.ToInt16(this.portTextBox.Text),
                    (bool)this.anonymeCheckBox.IsChecked, this.loginTextBox.Text, this.passwordTextBox.Password);
                project.Smtp = smtp;
            }
            catch
            {
            }
        }

        private void populateFields()
        {
            Project project = App.MyConfig.OurProject;
            if (project.Smtp != null)
            {
                MailSettings smtp = project.Smtp;
                this.smtpTextBox.Text = smtp.Smtp;
                this.portTextBox.Text = smtp.Port.ToString();
                this.anonymeCheckBox.IsChecked = smtp.IsAnonymous;
                if (!smtp.IsAnonymous)
                {
                    this.loginTextBox.Text = smtp.Login;
                    this.passwordTextBox.Password = smtp.Password;
                    enableCredentials();
                }
                else
                    disableCredentials();
            }

            if (project.Email != null)
            {
                this.emailTextBox.Text = project.Email;
            }

        }

        private void passwordTextBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (!isInit)
                enableButtons();
        }

       
    }
}
